// pages/addAddress/addAddress.js
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    multiArray: [
      [''],
      [''],
      ['']
    ],
    objectMultiArray: [],
    multiIndex: [0, 0, 0],
    provincesList: {},
    cityList: {},
    districtsList: {},
    mobile: '',
    provincesId: '',
    cityId: '',
    districtsId: ''
  },

  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        this.areaCities(e.detail.value)
        break;
      case 1:
        this.areaCityId(e.detail.value)
        break;
    }
    this.setData(data);
  },

  // 省
  areaProvinces() {
    return api.areaProvinces({
      data: {},
      success: res => {
        if (res.code * 1 === 0) {
          let data = res.data.map(item => {
            return item.name
          })
          let provincesId = this.data.provincesId
          let provincesIndex = 0
          if (provincesId) {
            res.data.forEach((item, index) => {
              if (item.id * 1 === provincesId * 1) {
                provincesIndex = index
              }
            })
          }
          this.setData({
            multiArray: [data, [],
              []
            ],
            provincesList: res.data,
            multiIndex: [provincesIndex, 0, 0]
          }, () => {
            this.areaCities(provincesIndex)
          })
        }
      }
    })
  },
  // 市
  areaCities(index = 0) {
    let provinceId = this.data.provincesList[index]['id']

    return api.areaCities({
      data: {
        provinceId: provinceId
      },
      success: res => {
        if (res.code * 1 === 0) {
          let data = res.data.map(item => {
            return item.name
          })
          let cityId = this.data.cityId
          let cityIndex = 0
          if (cityId) {
            res.data.forEach((item, index) => {
              if (item.id * 1 === cityId * 1) {
                cityIndex = index
              }
            })
          }
          this.setData({
            multiArray: [this.data.multiArray[0], data, []],
            cityList: res.data,
            multiIndex: [this.data.multiIndex[0], cityIndex, 0]
          }, () => {
            this.areaCityId(cityIndex)
          })
        }
      }
    })
  },
  // 区
  areaCityId(index) {
    let cityId = this.data.cityList[index]['id']
    api.areaCityId({
      data: {
        cityId: cityId
      },
      success: res => {
        if (res.code * 1 === 0) {
          let data = res.data.map(item => {
            return item.name
          })
          let districtsId = this.data.districtsId
          let districtsIndex = 0
          if (districtsId) {
            res.data.forEach((item, index) => {
              if (item.id * 1 === districtsId * 1) {
                districtsIndex = index
              }
            })
          }
          this.setData({
            multiArray: [this.data.multiArray[0], this.data.multiArray[1], data],
            districtsList: res.data,
            multiIndex: [this.data.multiIndex[0], this.data.multiIndex[1], districtsIndex]
          })
        }
      }
    })
  },

  formSubmit(e) {
    let data = e.detail.value
    if (data.address === '') {
      wx.showToast({
        title: '请输入收货人',
      })
      return
    }
    if (data.mobile === '') {
      wx.showToast({
        title: '请输入手机号',
      })
      return
    }
    if (data.address === '') {
      wx.showToast({
        title: '请输入详细地址',
      })
      return
    }
    data.thirdAreaId = this.data.districtsList[data.thirdAreaId[2]]['id']
    data.id = this.data.id || ''
    api.addressEdit({
      data,
      success: res => {
        if (res.code * 1 === 0) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.msg
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.areaProvinces()
    let data = wx.getStorageSync('addressEditItem')
    console.log(data, 'data=========================')
    let provincesId = data.areaArray[0]['id'] || ''
    let cityId = data.areaArray[1]['id'] || ''
    let districtsId = data.areaArray[2]['id'] || ''
    this.setData({
      mobile: data.mobile || wx.getStorageSync('mobile'),
      name: data.name,
      address: data.address,
      id: data.id,
      provincesId: provincesId,
      cityId: cityId,
      districtsId: districtsId
    })
  }

})